float4x4 World;
float4x4 View;
float4x4 Projection;

float4 ModelColor;
float3 LightPos;

float bias;

struct VertexShaderInput
{
    float4 Position : POSITION0;
    float3 Normal : NORMAL0;
};

struct VertexShaderOutput
{
    float4 Position : POSITION0;
    float3 WorldNormal : TEXCOORD0;
    float3 WorldLightDir : TEXCOORD1;
};

VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
    VertexShaderOutput output;

    float4 worldPosition = mul(input.Position, World);
    float3 worldNormal = mul(float4(input.Normal, 1.0), World).xyz;
    float4 viewPosition = mul(worldPosition, View);
    output.Position = mul(viewPosition, Projection);
    output.Position.z -= bias;

    output.WorldNormal = normalize(worldNormal);
    output.WorldLightDir = normalize(LightPos - worldPosition);

    return output;
}

float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
    float3 ldir = normalize(input.WorldLightDir);
    float3 wnorm = normalize(input.WorldNormal);

    float diffuse = dot(ldir, wnorm);

    if (diffuse < 0.0)
    {
        diffuse = 0.0;
    }
    
    return float4( 0.8, 0.8, 0.8, 1.0) * diffuse + float4(0.2, 0.1, 0.1, 0.0);
}

technique Technique1
{
    pass Pass1
    {
        // TODO: set renderstates here.

        VertexShader = compile vs_3_0 VertexShaderFunction();
        PixelShader = compile ps_3_0 PixelShaderFunction();
    }
}
